@echo off

REM Author: Sen Du
REM Email: dusen.me@gmail.com
REM Created: 2024-04-13 20:00:00
REM Updated: 2024-04-13 20:00:00

setlocal enabledelayedexpansion

REM 设置路径
set FILEPATHS=("C:\Users\dusen\Downloads\temp\untitled")

REM 主函数
:main
for %%FILEPATH in (%FILEPATHS%) do (
    
    REM 查找符合条件的视频文件 (.mp4, .MP4, .MTS, .mov) 大于100MB
    for /r %%f in (*.mp4 *.MP4 *.MTS *.mov) do (
        set "file=%%~ff"
        set "filesize=%%~zf"
        
        REM 检查文件大小是否超过100MB (104857600字节)
        if !filesize! GTR 104857600 (
            set "newFile=!file:~0,-4!_1080p.mp4"

            echo [!date:~10,4!!date:~4,2!!date:~7,2!!time:~0,2!!time:~3,2!!time:~6,2!] reduce start...
            echo OldFile: !file!
            echo newFile: !newFile!

            REM 使用 HandBrakeCLI 进行视频压缩
            HandBrakeCLI -i "!file!" -o "!newFile!" --preset-import-gui --preset="Very Fast 1080p30"
            
            if !errorlevel! equ 0 (
                REM 删除原文件
                del /f "!file!"
                echo [!date:~10,4!!date:~4,2!!date:~7,2!!time:~0,2!!time:~3,2!!time:~6,2!] reduce end.
            ) else (
                echo [!date:~10,4!!date:~4,2!!date:~7,2!!time:~0,2!!time:~3,2!!time:~6,2!] reduce failed!
            )
        )
    )
)
goto :eof
